﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DeltaFlex.Negoci;

namespace Vista.Controls
{
    public partial class AltaClient : System.Web.UI.UserControl
    {
        #region Variables Globals
        public string NomClient
        {
            get
            {
                return (Convert.ToString(ViewState["_nomClient"]) == string.Empty ?
                    "-" : Convert.ToString(ViewState["_nomClient"]));
            }
            set { ViewState["_nomClient"] = value; }
        }

        public string DNI_NIE
        {
            get
            {
                return (Convert.ToString(ViewState["_dni_nie"]) == string.Empty ?
                    "-" : Convert.ToString(ViewState["_dni_nie"]));
            }
            set { ViewState["_dni_nie"] = value; }
        }

        public string Direccio
        {
            get
            {
                return (Convert.ToString(ViewState["_direccio"]) == string.Empty ?
                    "-" : Convert.ToString(ViewState["_direccio"]));
            }
            set { ViewState["_direccio"] = value; }
        }

        public string Poligon
        {
            get
            {
                return (Convert.ToString(ViewState["_poligon"]) == string.Empty ?
                    "-" : Convert.ToString(ViewState["_poligon"]));
            }
            set { ViewState["_poligon"] = value; }
        }

        public string Ciutat
        {
            get
            {
                return (Convert.ToString(ViewState["_ciutat"]) == string.Empty ?
                    "-" : Convert.ToString(ViewState["_ciutat"]));
            }
            set { ViewState["_ciutat"] = value; }
        }

        public int CodiPostal
        {
            get
            {
                return (Convert.ToInt32(ViewState["_codiPostal"]) == 0 ?
                    0 : Convert.ToInt32(ViewState["_codiPostal"]));
            }
            set { ViewState["_codiPostal"] = value; }
        }

        public string Provincia
        {
            get
            {
                return (Convert.ToString(ViewState["_provincia"]) == string.Empty ?
                    "-" : Convert.ToString(ViewState["_provincia"]));
            }
            set { ViewState["_provincia"] = value; }
        }

        public string Pais
        {
            get
            {
                return (Convert.ToString(ViewState["_pais"]) == string.Empty ?
                    "-" : Convert.ToString(ViewState["_pais"]));
            }
            set { ViewState["_pais"] = value; }
        }

        public int Telefon
        {
            get
            {
                return (Convert.ToInt32(ViewState["_telefon"]) == 0 ?
                   0 : Convert.ToInt32(ViewState["_telefon"]));
            }
            set { ViewState["_telefon"] = value; }
        }

        public int Fax
        {
            get
            {
                return (Convert.ToInt32(ViewState["_fax"]) == 0 ?
                    0 : Convert.ToInt32(ViewState["_fax"]));
            }
            set { ViewState["_fax"] = value; }
        }

        public string Email
        {
            get
            {
                return (Convert.ToString(ViewState["_email"]) == string.Empty ?
                    "-" : Convert.ToString(ViewState["_email"]));
            }
            set { ViewState["_email"] = value; }
        }

        public string NumCompte
        {
            get
            {
                return (Convert.ToString(ViewState["_numCompte"]) == string.Empty ?
                    "-" : Convert.ToString(ViewState["_numCompte"]));
            }
            set { ViewState["_numCompte"] = value; }
        }

        public string FormaPago
        {
            get
            {
                return (Convert.ToString(ViewState["_formaPago"]) == string.Empty ?
                    "-" : Convert.ToString(ViewState["_formaPago"]));
            }
            set { ViewState["_formaPago"] = value; }
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region Carrega a memoria
        /// <summary>
        /// Carrega el contingut dels text Box del control Alta Client
        /// </summary>
        private void CarregarMemoria()
        {
            NomClient = txtNomClient.Text;
            DNI_NIE = txtNIF.Text;
            Direccio = txtCarrerNumero.Text;
            Poligon = txtPoligon.Text;
            Ciutat = txtCiutat.Text;
            CodiPostal = txtCodiPostal.Text == string.Empty ? 0 : Convert.ToInt32(txtCodiPostal.Text);
            Provincia = txtProvincia.Text;
            Pais = txtPais.Text;
            Telefon = txtTelefon.Text == string.Empty ? 0 : Convert.ToInt32(txtTelefon.Text);
            Fax = txtFax.Text == string.Empty ? 0 : Convert.ToInt32(txtFax.Text);
            Email = txtEmail.Text;
            NumCompte = txtNumCompte.Text;
            FormaPago = txtFormaPago.Text;
        }
        #endregion
        
        #region Neteja Controls
        /// <summary>
        ///Neteja tots els controls del formulari.
        /// </summary>
        /// <param name="controles"></param>
        public void NetejaControls(ControlCollection controles)
        {
            foreach (Control control in controles)
            {
                if (control is TextBox)
                    ((TextBox)control).Text = string.Empty;
                else if (control is DropDownList)
                    ((DropDownList)control).ClearSelection();
                else if (control is RadioButtonList)
                    ((RadioButtonList)control).ClearSelection();
                else if (control is CheckBoxList)
                    ((CheckBoxList)control).ClearSelection();
                else if (control is RadioButton)
                    ((RadioButton)control).Checked = false;
                else if (control is CheckBox)
                    ((CheckBox)control).Checked = false;
                else if (control.HasControls())
                    //Esta linea detecta un Control que contigui altres Controls
                    //Així cap control es quedarà sense netejar
                    NetejaControls(control.Controls);
            }
        }
        #endregion

        #region Guardar
        protected void btnAltaClient_Click(object sender, EventArgs e)
        {
            string missatge, exepcio;
            missatge = exepcio = string.Empty;
            CarregarMemoria();
            try
            {
                Client.InsertarClient(NomClient, DNI_NIE, Telefon, Fax, Email, NumCompte, FormaPago,
                      Direccio, Poligon, Ciutat, CodiPostal, Provincia, Pais);
            }
            catch (Exception ex)
            {
                exepcio = "Error en donar d'alta un nou client <br/> " + ex.Message;
            }

            if (String.IsNullOrEmpty(exepcio))
                missatge = ConstantsAltes.mplbGuardarClientOK;
            else
                missatge = ConstantsAltes.mplbGuardarClientKO + " <br/> " + exepcio;

            mpAltaClient.LabelHeader = ConstantsAltes.mplhGuardarClient;
            mpAltaClient.LabelBody = missatge;
            mpAltaClient.MostraInfo = true;
            
            NetejaControls(this.Controls);

            mpAltaClient.MostrarModal();
        }
        #endregion
    }
}